package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.dto.DishDto;
import com.itheima.pojo.Category;
import com.itheima.pojo.Dish;
import com.itheima.pojo.DishFlavor;
import com.itheima.service.CategoryService;
import com.itheima.service.DishFlavorService;
import com.itheima.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private DishService dishService;
    @Autowired
    private CategoryService categoryService;

    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        Page<Dish> dishPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();

        qw.like(StringUtils.isNotBlank(name),Dish::getName,name);
        qw.orderByAsc(Dish::getPrice);
        Page<Dish> page1 = dishService.page(dishPage, qw);
        Page<DishDto> dishDtoPage = new Page<>(page, pageSize);
        List<DishDto> dishDtolist = new ArrayList<>();
        List<Dish> records = page1.getRecords();//得到了一个dish数组
        for (Dish dish : records) {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish,dishDto);//将dish的数据复制到dishDto
            Category category = categoryService.getById(dishDto.getCategoryId());
            dishDto.setCategoryName(category.getName());
            dishDtolist.add(dishDto);
        }
        dishDtoPage.setRecords(dishDtolist);
        BeanUtils.copyProperties(dishPage,dishDtoPage,"records");
        //由于dish数据转换为了dishDto，为了records中的数据集合不被复制，可以进行属性的忽略
        //把total等必须分页查找能得到的数据复制给dishDtoPage

        return R.success(dishDtoPage);
    }
    @GetMapping("/{id}")
    public R<Dish> getById(@PathVariable long id){
        DishDto dishDto = dishService.selectDishDto(id);
        return R.success(dishDto);
    }
    @PostMapping
    public R<String> add(@RequestBody DishDto dishDto){
//        for (DishFlavor dishFlavor : list) {
//            dishFlavorService.save(dishFlavor);
//        }
//        dishService.save(dish);
        dishService.saveWithFlavor(dishDto);
        String key = "dish" + dishDto.getCategoryId();
        redisTemplate.delete(key);
        return R.success("添加成功");

    }
    @PutMapping R<String> update(@RequestBody DishDto dishDto){
        dishService.updateWithFlavor(dishDto);
        String key = "dish" + dishDto.getCategoryId();
        redisTemplate.delete(key);
        return R.success("修改成功");
    }
    @PostMapping("status/{status}")
    public R<String> statusUpdate(@PathVariable Integer status,@RequestParam List<Long> ids){
        for (Long id : ids) {
            Dish dish = new Dish();

            dish.setStatus(status);
            dish.setId(id);
            dishService.updateById(dish);
        }
        return R.success(status==0?"禁售成功":"启售成功");
    }
    @DeleteMapping
    public R<String> deleteById(@RequestParam List<Long> ids){

        dishService.removeByIdsWithFlavor(ids);
        return R.success("删除成功");
    }
    @GetMapping("/list")
    public R<List<DishDto>> getByCategoryId(Dish dish){
        String key = "dish" + dish.getCategoryId();
        List<DishDto> dishDtos = redisTemplate.opsForList().range(key,0,-1);
        if (dishDtos==null||dishDtos.size()==0){
            dishDtos = dishService.getDishWithFlavor(dish);
            redisTemplate.opsForList().rightPushAll(key,dishDtos);
        }

        return R.success(dishDtos);
    }

}
